package homework;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class HomeWork_1 {

    static List<StudentBean> studentsInfo = new ArrayList<StudentBean>();

    static{
        studentsInfo.add(new StudentBean(1,"李菊花","男",19));
        studentsInfo.add(new StudentBean(2,"王菊花","女",20));
        studentsInfo.add(new StudentBean(3,"张菊花","男",22));
        studentsInfo.add(new StudentBean(4,"刘菊花","女",24));
        studentsInfo.add(new StudentBean(5,"赵菊花","男",26));
        studentsInfo.add(new StudentBean(6,"周菊花","男",20));
    }

    public static void main(String[] args){

        File copyFile = new File("E:\\javaooGit\\javaooDay21\\src\\homework\\student.txt");

        if(!copyFile.exists()){
            try {
                copyFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String copyMsg = "";

       String ss[] =  studentsInfo.get(0).toString().substring(studentsInfo.get(0).toString().indexOf("{")+1,
               studentsInfo.get(0).toString().indexOf("}")).split(",");
        for (String s : ss) {
           copyMsg +=  s.split("=")[0] + "\t";
        }


    //    System.out.println(copyMsg);

        for (StudentBean student : studentsInfo) {
            copyMsg += "\n" + "\t";
            String[] studentMsgs = student.toString().substring(student.toString().indexOf("{")+1,
                    student.toString().indexOf("}")).split(",");

            for (String studentMsg : studentMsgs) {
                copyMsg = copyMsg  + studentMsg.split("=")[1].replace("'", "") + "\t\t";
            }
        }

        //System.out.println(copyMsg);
        try(
            Writer w = new FileWriter(copyFile, true)
        ){
            w.write(copyMsg);
            w.flush();
        }catch (Exception e){

        }

        System.out.println("OK");
    }
}
